package com.nebula.dao.impl;

import com.nebula.dao.Database;
import com.nebula.dao.IBenefitsDao;
import com.nebula.entity.Benefits;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author CcccX
 * @since 2020-07-05 20:18
 */
public class BenefitsDaoImpl extends BaseDaoImpl<Benefits> implements IBenefitsDao {
    @Override
    public BigDecimal getTotalSalary(String userId, Object... conditions) {
        String sql = "select sum(salary) from benefits where user_id = ?";
        sql = handle(sql, conditions);
        BigDecimal dc = BigDecimal.valueOf(0);
        ResultSet rs = (ResultSet) Database.excute(sql, userId);
        try {
            if (rs != null) {
                while (rs.next()) {
                    dc = rs.getBigDecimal(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dc;
    }

    private String handle(String sql, Object[] objects) {
        if (objects != null) {
            for (Object object : objects) {
                sql += object;
            }
        }
        return sql;
    }
}
